

package myBean;

import SessionBean.SessionBeanTbAdminRemote;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author CHINHNB
 */
@ManagedBean
@SessionScoped
public class ManagedBeanAdmin {
    private String username;
    private String password;
    private String result;    

    public SessionBeanTbAdminRemote getTbAdminRemote(){
        SessionBeanTbAdminRemote tbAdmin=null;
        try {
            Context c = new InitialContext();
            tbAdmin=(SessionBeanTbAdminRemote)c.lookup(SessionBeanTbAdminRemote.class.getName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return tbAdmin;
    }

    public String checkLogin(){
        SessionBeanTbAdminRemote sr=getTbAdminRemote();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
        if(sr.bmCheckLogin(username, password)){
            result="index.xhtml";
        }
        else
        {
            result="login.xhtml";
            username="";
        }        
        session.setAttribute("login", result);
        return result;
    }   
    /**
     * @return the username
     */
    public String getUsername() {        
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {        
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {        
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {        
        this.password = password;
    }

    /**
     * @return the result
     */
    public String getResult() { 
        return result;
    }

    /**
     * @param result the result to set
     */
    public void setResult(String result) {
        this.result = result;
    }
   
}
